package com.bsbm.member.bean;

import com.bsbm.member.domain.MemberTypeEnum;
import com.bsbm.member.service.MemberService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SABIR IQBAL
 * Date: 2/9/14
 * Time: 11:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class MemberBean {

    private Long id;

    private MemberBean viewEditBean;

    private boolean viewMode;

    private String memberTypeCd;

    private List<SelectItem> memberTypeCdValues;

    private java.util.Date createdOn;

    private java.util.Date updatedOn;

    private String createdBy;

    private String firstName;

    private String middleName;

    private String lastName;

    private String fatherName;

    private Date dob;

    private String phoneNumber;

    private String emailId;

    private String idCardNo;

    private String formNo;

    private String occupationCd;

    private List<SelectItem> occupationCdValues;

    private String qualificationCd;

    private List<SelectItem> qualificationCdValues;

    private String stateCd;

    private List<SelectItem> stateCdValues;

    private String rural;

    private String districtCd;

    private List<SelectItem> districtCdValues;

    private String townCd;

    private List<SelectItem> townCdValues;

    private String blockCd;

    private List<SelectItem> blockCdValues;

    private String panchayatCd;

    private List<SelectItem> panchayatCdValues;

    private String addressLine1;

    private String addressLine2;

    private String pinCode;

    private String village;

    private List<MemberBean> memberBeanList;

    private MemberService memberService;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMemberTypeCd() {
        return memberTypeCd;
    }

    public void setMemberTypeCd(String memberTypeCd) {
        this.memberTypeCd = memberTypeCd;
    }

    public List<SelectItem> getMemberTypeCdValues() {
        if(CollectionUtils.isEmpty(memberTypeCdValues)){
            memberTypeCdValues = new ArrayList<SelectItem>();
            memberTypeCdValues.add(new SelectItem(MemberTypeEnum.Associate.getText(), MemberTypeEnum.Associate.getText()));
            memberTypeCdValues.add(new SelectItem(MemberTypeEnum.Member.getText(), MemberTypeEnum.Member.getText()));
        }
        return memberTypeCdValues;
    }

    public void setMemberTypeCdValues(List<SelectItem> memberTypeCdValues) {
        this.memberTypeCdValues = memberTypeCdValues;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public Date getUpdatedOn() {
        return updatedOn;
    }

    public void setUpdatedOn(Date updatedOn) {
        this.updatedOn = updatedOn;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getIdCardNo() {
        return idCardNo;
    }

    public void setIdCardNo(String idCardNo) {
        this.idCardNo = idCardNo;
    }

    public String getFormNo() {
        return formNo;
    }

    public void setFormNo(String formNo) {
        this.formNo = formNo;
    }

    public String getOccupationCd() {
        return occupationCd;
    }

    public void setOccupationCd(String occupationCd) {
        this.occupationCd = occupationCd;
    }

    public List<SelectItem> getOccupationCdValues() {
        if(CollectionUtils.isEmpty(occupationCdValues)) {
            occupationCdValues = new ArrayList<SelectItem>();
            occupationCdValues.add(new SelectItem("EMP","Employed"));
            occupationCdValues.add(new SelectItem("BUS","Business"));
            occupationCdValues.add(new SelectItem("OTH","Other"));
        }
        return occupationCdValues;
    }

    public void setOccupationCdValues(List<SelectItem> occupationCdValues) {
        this.occupationCdValues = occupationCdValues;
    }

    public String getQualificationCd() {
        return qualificationCd;
    }

    public void setQualificationCd(String qualificationCd) {
        this.qualificationCd = qualificationCd;
    }

    public List<SelectItem> getQualificationCdValues() {
        if(CollectionUtils.isEmpty(qualificationCdValues)) {
            qualificationCdValues = new ArrayList<SelectItem>();
            qualificationCdValues.add(new SelectItem("B8TH","Below 8th Standard"));
            qualificationCdValues.add(new SelectItem("8TH","8th Standard"));
            qualificationCdValues.add(new SelectItem("10TH","Madhymik"));
            qualificationCdValues.add(new SelectItem("12TH","H.S."));
            qualificationCdValues.add(new SelectItem("B","Bachelor"));
            qualificationCdValues.add(new SelectItem("M","Master Degree"));
        }
        return qualificationCdValues;
    }

    public void setQualificationCdValues(List<SelectItem> qualificationCdValues) {
        this.qualificationCdValues = qualificationCdValues;
    }

    public String getStateCd() {
        return stateCd;
    }

    public void setStateCd(String stateCd) {
        this.stateCd = stateCd;
    }

    public String getRural() {
        return rural;
    }

    public void setRural(String rural) {
        this.rural = rural;
    }

    public String getDistrictCd() {
        return districtCd;
    }

    public void setDistrictCd(String districtCd) {
        this.districtCd = districtCd;
    }

    public List<SelectItem> getDistrictCdValues() {
        if(CollectionUtils.isEmpty(districtCdValues)) {
            districtCdValues = new ArrayList<SelectItem>();
            districtCdValues.add(new SelectItem("BA","Bankura"));
            districtCdValues.add(new SelectItem("KO","Kolkata"));
            districtCdValues.add(new SelectItem("MI","Midnapur"));
        }

        return districtCdValues;
    }

    public void setDistrictCdValues(List<SelectItem> districtCdValues) {
        this.districtCdValues = districtCdValues;
    }

    public String getTownCd() {
        return townCd;
    }

    public void setTownCd(String townCd) {
        this.townCd = townCd;
    }

    public List<SelectItem> getTownCdValues() {
        return townCdValues;
    }

    public void setTownCdValues(List<SelectItem> townCdValues) {
        this.townCdValues = townCdValues;
    }

    public String getBlockCd() {
        return blockCd;
    }

    public void setBlockCd(String blockCd) {
        this.blockCd = blockCd;
    }

    public List<SelectItem> getBlockCdValues() {
        return blockCdValues;
    }

    public void setBlockCdValues(List<SelectItem> blockCdValues) {
        this.blockCdValues = blockCdValues;
    }

    public String getPanchayatCd() {
        return panchayatCd;
    }

    public void setPanchayatCd(String panchayatCd) {
        this.panchayatCd = panchayatCd;
    }

    public List<SelectItem> getPanchayatCdValues() {
        return panchayatCdValues;
    }

    public void setPanchayatCdValues(List<SelectItem> panchayatCdValues) {
        this.panchayatCdValues = panchayatCdValues;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public String getPinCode() {
        return pinCode;
    }

    public void setPinCode(String pinCode) {
        this.pinCode = pinCode;
    }

    public String getVillage() {
        return village;
    }

    public void setVillage(String village) {
        this.village = village;
    }

    public void selectRadio(ValueChangeEvent event) {

        System.out.println("Inside select radio");
    }

    public List<SelectItem> getStateCdValues() {
        if(stateCdValues == null){
            stateCdValues = new ArrayList<SelectItem>();
            stateCdValues.add(new SelectItem("WB","West Bengal"));
        }
        return stateCdValues;
    }

    public void setStateCdValues(List<SelectItem> stateCdValues) {
        this.stateCdValues = stateCdValues;
    }

    public void districtChanged(ValueChangeEvent event) {
        if("Y".equals(rural)){
           if(townCdValues == null){
               townCdValues = new ArrayList<SelectItem>();
           }else {
               townCdValues.clear();
           }
            if (null != event.getNewValue()) {
                if (((String) event.getNewValue()).equals("BA")) {
                    townCdValues.add(new SelectItem("BIS","Bishnupur"));
                    townCdValues.add(new SelectItem("SON","Sonamukhi"));
                    townCdValues.add(new SelectItem("BAN","Bankura"));
                } else if(((String) event.getNewValue()).equals("KO")) {
                    townCdValues.add(new SelectItem("KOL","Kolkata"));
                }else if(((String) event.getNewValue()).equals("MI")){
                    townCdValues.add(new SelectItem("MID","Midnapur"));
                    townCdValues.add(new SelectItem("KGP","Khagrapur"));
                }
            }

        }
    }

    public MemberService getMemberService() {
        return memberService;
    }

    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    public List<MemberBean> getMemberBeanList() {
        return memberBeanList;
    }

    public void setMemberBeanList(List<MemberBean> memberBeanList) {
        this.memberBeanList = memberBeanList;
    }

    public MemberBean getViewEditBean() {
        return viewEditBean;
    }

    public void setViewEditBean(MemberBean viewEditBean) {
        this.viewEditBean = viewEditBean;
    }

    public boolean isViewMode() {
        return viewMode;
    }

    public void setViewMode(boolean viewMode) {
        this.viewMode = viewMode;
    }

    public String save() {
     try{
        memberService.create(this);
         return "success";
     }catch (RuntimeException e) {
         return "error";
     }
    }

    public void searchValidation(ComponentSystemEvent event){
        FacesContext fc = FacesContext.getCurrentInstance();
        UIComponent components = event.getComponent();
        UIInput uiInputFirstNameTextField = (UIInput) components.findComponent("firstNameTextField");
        UIInput uiInputMiddleNameTextField = (UIInput) components.findComponent("middleNameTextField");
        UIInput uiInputLastNameTextField = (UIInput) components.findComponent("lastNameTextField");
        UIInput uiInputIdCardNoTextField = (UIInput) components.findComponent("idCardNoTextField");
        UIInput uiInputFormNoTextField = (UIInput) components.findComponent("formNoTextField");

        if(!StringUtils.hasText((String) uiInputFirstNameTextField.getLocalValue()) && !StringUtils.hasText((String) uiInputMiddleNameTextField.getLocalValue())
                && !StringUtils.hasText((String)uiInputLastNameTextField.getLocalValue()) && !StringUtils.hasText((String)uiInputFormNoTextField.getLocalValue())
                && !StringUtils.hasText((String)uiInputIdCardNoTextField.getLocalValue())){
            FacesMessage msg = new FacesMessage("Please provide at least one information");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            UIInput uiInputHiddenFieldForErrorMessage = (UIInput) components.findComponent("hiddenFieldForErrorMessage");
            fc.addMessage(uiInputHiddenFieldForErrorMessage.getClientId(), msg);
            fc.renderResponse();
        }
    }

    public String search(){
        List<MemberBean> result = new ArrayList<MemberBean>();
        try{
            this.memberBeanList = memberService.search(this);
            return "success";
        }catch (RuntimeException e) {
            return "error";
        }
    }

    public String view(String id){
        try{
            this.viewEditBean = memberService.findById(id);
            this.setViewMode(true);
        }catch(RuntimeException e){
            return "error";
        }

        return "ViewMember";
    }
    public String edit(String id){
        try{
            this.viewEditBean = memberService.findById(id);
            this.setViewMode(false);
        }catch(RuntimeException e){
            return "error";
        }
        return "EditMember";
    }

    public String delete(String id) {
        try{
            memberService.delete(id);
            memberBeanList = Collections.EMPTY_LIST;
            this.memberBeanList = memberService.search(this);

        }catch(RuntimeException e){
            return "error";
        }
        return "success";
    }

}
